package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import com.itheima.utils.LockUtils;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 体检预约服务
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //体检预约
    @Transactional
    public Result order(Map map) {
        /**
         1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
         2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
         3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
         4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
         5、预约成功，更新当日的已预约人数
         */
        //获取日期
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if (orderSetting == null) {
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //所选日期已经约满，无法预约
            return new Result(false, MessageConst.ORDER_FULL);
        }
        //判断是否在重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            Integer memberId = member.getId();//会员id
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));//套餐id
            //这里需要调整 Order类的 构造方法
            Order order = new Order(memberId, date, setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList != null && orderList.size() > 0) {
                //用户在重复预约，不能完成预约
                return new Result(false, MessageConst.HAS_ORDERED);
            }
        }
        if (member == null) {
            //当前用户不是会员，需要自动完成注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //保存预约信息
        Order order = new Order(member.getId(),
                date,
                (String) map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        //更新已预约人数
        InterProcessLock lock = LockUtils.getLock();
        try {
            lock.acquire();
            orderSetting.setReservations(orderSetting.getReservations() + 1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lock.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConst.ORDER_SUCCESS, order.getId());
    }

    //根据id查询预约信息，包括体检人信息、套餐信息
    public Map findById(Integer id) {
        Map map = orderDao.findById(id);
        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }

    @Override


    public void TestLock() {
        for (int i = 0; i < 300; i++) {
            new Thread((new Runnable() {
                @Override
                public void run() {
//                    InterProcessLock lock = null;
                    try {
//                        lock = LockUtils.getLock();
//                        lock.acquire();
                        String dateStr = "2019-05-11";
                        Date date = DateUtils.parseString2Date(dateStr);
                        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
                        orderSetting.setReservations(orderSetting.getReservations() + 1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
//                            lock.release();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            })).start();
        }
    }
}